package org.ghost.consul.client.controller;

import org.ghost.consul.common.response.HttpRspDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by yarnliu on 2017-10-27.
 */
@RequestMapping("hello")
@RestController
public class HelloController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/world1", method = RequestMethod.GET)
    public HttpRspDTO getWorld1() {
        return this.restTemplate.getForObject(loadBalancerClient.choose("helloService").getUri().toString() + "/hello/world", HttpRspDTO.class);
    }

    @RequestMapping(value = "/world2", method = RequestMethod.GET)
    public HttpRspDTO getWorld2() {
        return this.restTemplate.getForObject(discoveryClient.getInstances("helloService").get(0).getUri().toString() + "/hello/world", HttpRspDTO.class);
    }
}
